package com.jinbo.footmessage.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtils {


    public static String getWeek(Calendar c) {
        int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
        switch (dayOfWeek) {
            case 1:
                return "星期日";
            case 2:
                return "星期一";
            case 3:
                return "星期二";
            case 4:
                return "星期三";
            case 5:
                return "星期四";
            case 6:
                return "星期五";
            case 7:
                return "星期六";
        }

        return "";
    }

    public static String getEndTime(String startTime, int time) {
        String[] strs = startTime.split(":");
        int hour = Integer.valueOf(strs[0]);
        int minute = Integer.valueOf(strs[1]) + time;
        while (minute >= 60) {
            minute = minute - 60;
            hour = hour + 1;
        }
        String minStr = minute + "";
        String houStr = hour + "";
        if (hour < 10)
            houStr = "0" + hour;
        if (minute < 10)
            minStr = "0" + minute;
        return houStr + ":" + minStr;
    }
/*
   2014-4-7变为2014-04-07
 */


    public static String getDate(String date) {
        String[] strs = date.split("-");
        String yeaStr = strs[0];
        int month = Integer.valueOf(strs[1]);
        int day = Integer.valueOf(strs[2]);
        String monStr = month + "";
        String dayStr = day + "";
        if (month < 10)
            monStr = "0" + month;
        if (day < 10)
            dayStr = "0" + day;
        return yeaStr + "-" + monStr + "-" + dayStr;
    }
    //统一时间格式
    public static final String FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT = "yyyy-MM-dd";
    public static final String NO_SECOND_FORMAT = "yyyy-MM-dd HH:mm";


    /**
     * 格式化时间
     *
     * @param
     * @param
     * @return String
     */
    public static String dateToString(Date date, String strFormat) {
        String time;
        SimpleDateFormat format = new SimpleDateFormat(strFormat);
        time = format.format(date);
        return time;
    }
/*
* * 获取格式化后的当前时间 格式为：yyyy-MM-dd HH:mm:ss
    *
    * @return
    *
    * */

    public static String getNowTimeFormat() {

        Calendar calendar = Calendar.getInstance();
        return dateToString(calendar.getTime(), FORMAT);
    }

/*
* * 获取格式化后的当前时间 格式为：yyyy-MM-dd
    *
    * @return
    *
    * */

    public static String getNowDateFormat() {

        Calendar calendar = Calendar.getInstance();
        return dateToString(calendar.getTime(), DATE_FORMAT);
    }






    /*
    下面的还没用到
     */
/*
    public static boolean isNight() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date(System.currentTimeMillis()));
        int hour = c.get(Calendar.HOUR_OF_DAY);
        if (hour < 6 || hour >= 18) {
            return true;
        }
        return false;
    }

    public static String getMonthAndDateAndWeek(Calendar c) {
        return getMonthAndDate(c) + " " + getWeek(c);
    }

    public static String getMonthAndDate(Calendar c) {
        int month = c.get(Calendar.MONTH) + 1;
        int dayOfMonth = c.get(Calendar.DAY_OF_MONTH);

        return month + "." + dayOfMonth;
    }



    public static String getMonthAndDateAndWeek() {
        return getMonthAndDate() + " " + getWeek();
    }

    public static String getMonthAndDate() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date(System.currentTimeMillis()));
        int month = c.get(Calendar.MONTH) + 1;
        int dayOfMonth = c.get(Calendar.DAY_OF_MONTH);

        return month + "." + dayOfMonth;
    }

    public static String getWeek() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date(System.currentTimeMillis()));
        int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
        switch (dayOfWeek) {
            case 1:
                return "星期日";
            case 2:
                return "星期一";
            case 3:
                return "星期二";
            case 4:
                return "星期三";
            case 5:
                return "星期四";
            case 6:
                return "星期五";
            case 7:
                return "星期六";
        }

        return "";
    }

    *//*
    * 取本周7天的第一天（周一的日期）
    *//*
    public static String getNowWeekBegin() {
        int mondayPlus;
        Calendar cd = Calendar.getInstance();
        // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; // 因为按中国礼拜一作为第一天所以这里减1
        if (dayOfWeek == 1) {
            mondayPlus = 0;
        } else {
            mondayPlus = 1 - dayOfWeek;
        }
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.add(GregorianCalendar.DATE, mondayPlus);
        Date monday = currentDate.getTime();

        String preMonday = dateToString(monday, DATE_FORMAT);

        return transformTimeToServer(preMonday);
    }

    private static String transformTimeToServer(String clientTime) {
        return clientTime.replace(" ", "T");
    }


    //============================ljj======================================
    //统一时间格式
    public static final String FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT = "yyyy-MM-dd";
    public static final String NO_SECOND_FORMAT = "yyyy-MM-dd HH:mm";

    *//**
     * 时间戳转换成时间格式
     *
     * @param time
     * @return
     *//*
    public static String formatTime(long time) {
        Date date = new Date(time);
        return dateToString(date, FORMAT);
    }

    *//**
     * 根据传入的date 返回时间
     * @param date
     * @return
     *//*
    public static String formatTime(Date date){
        return dateToString(date, FORMAT);
    }





    *//**
     * 时间类型的转换
     * @param serverTime
     * @param format
     * @return
     *//*
    public static long stringToLong(String serverTime, String format) {
        String date = serverTimeToClientStr(serverTime);
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            Date d = sdf.parse(date);
            return d.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    *//**
     * 时间类型的转换
     * @param date
     * @param format
     * @return
     *//*
    public static Date stringToData(String date, String format){
        Date tdate = null;
        try {
            SimpleDateFormat mFormatter = new SimpleDateFormat(format);
            tdate = mFormatter.parse(date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return tdate;
    }


    *//**
     * 时间类型的转换
     * @param time
     * @param format
     * @return
     *//*
    public static String longToString(long time, String format) {
        Date date = new Date(time);
        return dateToString(date, format);
    }


    public static String serverTimeToClientStr(String time) {
        String returnTime = time.replaceAll("T", " ");
        if (returnTime.split(".").length > 0) {
            returnTime = returnTime.split(".")[0];
        }
        return returnTime;
    }


    *//**
     * 获取处理后的时间
     *
     * @return
     *//*
    public static String getInterval(Date date) {
        // 定义最终返回的结果字符串。
        String interval = "";
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        long millisecond = Calendar.getInstance().getTimeInMillis()
                - date.getTime();

        long second = millisecond / 1000;

        if (second <= 0) {
            second = 0;
        }

        // if (second < 60 * 5) {
        // interval = "刚刚";
        // } else
        if (second < 60 * 10) {
            interval = "5分钟前";
        } else if (second < 60 * 30) {
            interval = "10分钟前";
        } else if (second < 60 * 60) {
            interval = "30分钟前";
        } else {

            int hour = c.get(Calendar.HOUR_OF_DAY);
            int day = c.get(Calendar.DAY_OF_MONTH);
            int month = c.get(Calendar.MONTH);
            int year = c.get(Calendar.YEAR);

            String AP = "";

            if (hour <= 12) {
                AP = "上午 ";
            } else {
                AP = "下午";
            }

            if (Calendar.getInstance().get(Calendar.YEAR) == year) {// 同年
                if (Calendar.getInstance().get(Calendar.MONTH) == month) {// 同月
                    switch (Calendar.getInstance().get(Calendar.DAY_OF_MONTH)
                            - day) {
                        case 0:// 当天

                            interval += AP;
                            interval += dateToString(date, " HH:mm");

                            break;

                        case 1:// 昨天

                            interval += "昨天 ";
                            interval += AP;

                            break;
                        case 2:// 前天

                            interval += "前天 ";
                            interval += AP;

                            break;

                        default:

                            interval = dateToString(date, "M-dd");

                            break;
                    }
                } else {
                    interval = dateToString(date, "M-dd");
                }

            } else {
                interval = dateToString(date, "yyyy-M-dd");
            }

        }
        return interval;
    }


    public static long serverTimeToClientLong(String serverTime) {
        String date = serverTimeToClientStr(serverTime);
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT);
        try {
            Date d = sdf.parse(date);
            return d.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }


    public static long getCurrentTime() {
        return System.currentTimeMillis();
    }

    *//**
     * 本地时间转换成版本号
     * @return
     *//*
    public static long getCurrentVersionTime() {
        return System.currentTimeMillis() + 8 * 3600 * 1000;
    }

    *//**
     * 获取当前时间的版本号
     * @param time
     * @return
     *//*
    public static long getVersionTime(long time) {
        return time + 8 * 3600 * 1000;
    }

    *//**
     * 版本号转换成客户端时间
     * @param time
     * @return
     *//*
    public static long versionToTime(long time){
        if (time == 0) {
            return System.currentTimeMillis() - 8 * 3600 * 1000;
        } else {
            return time - 8 * 3600 * 1000;
        }
    }

    *//**
     * 解释时间
     *
     * @param serverTime
     * @return
     *//*
    public static String parseTime(String serverTime) {
        try {
            String date = serverTimeToClientStr(serverTime);
            SimpleDateFormat sdf = new SimpleDateFormat(FORMAT);
            Date d = sdf.parse(date);
            return getInterval(d);
        } catch (Exception e) {
            e.printStackTrace();
            return "--";
        }
    }

    public static String getYearAndMonthAndDay(String serverTime) {
        try {
            String returnTime = serverTime.split("T")[0];
            return returnTime;
        } catch (Exception e) {
            e.printStackTrace();
            return "未知";
        }
    }


    private static SimpleDateFormat yearFormat = null;
    private static SimpleDateFormat monthFormat = null;
    private static SimpleDateFormat dayFormat = null;
    private static SimpleDateFormat allFormat = null;
    private static SimpleDateFormat projectFormat = null;


    *//**
     * 获取年份
     *
     * @param context
     * @param timeInMillis
     * @return
     *//*
    public static String getYear(Context context, long timeInMillis) {
        if (yearFormat == null) {
            yearFormat = new SimpleDateFormat("yyyy");
        }
        return yearFormat.format(new Date(timeInMillis));
    }


    *//**
     * 获取月份
     *
     * @param context
     * @param timeInMillis
     * @return
     *//*
    public static String getMonth(Context context, long timeInMillis) {
        if (monthFormat == null) {
            monthFormat = new SimpleDateFormat("MM");
        }
        return monthFormat.format(new Date(timeInMillis));
    }


    *//**
     * 获取日
     *
     * @param context
     * @param timeInMillis
     * @return
     *//*
    public static String getDay(Context context, long timeInMillis) {
        if (dayFormat == null) {
            dayFormat = new SimpleDateFormat("dd");
        }
        return dayFormat.format(new Date(timeInMillis));
    }


    *//**
     * 获取时间小时
     * @param timeInMillis
     * @return
     *//*
    public static int getHour(long timeInMillis){
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(timeInMillis);
        int hour = c.get(Calendar.HOUR_OF_DAY);
        return hour;
    }


    *//**
     * 获取现在时间的字符串
     *
     * @return
     *//*
    public static String getCurrentTimeStr() {
        long time = System.currentTimeMillis();
        if (allFormat == null) {
            allFormat = new SimpleDateFormat(FORMAT);
        }
        String serverTime = allFormat.format(new Date(time)).replace(" ", "T");
        return serverTime;
    }

    public static String getProjectTime(long timeMillis) {

        int currentYear = Calendar.getInstance().get(Calendar.YEAR);
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(timeMillis);
        int day = c.get(Calendar.DAY_OF_MONTH);
        int month = c.get(Calendar.MONTH) + 1;
        int year = c.get(Calendar.YEAR);
        if (currentYear == year) {
            return month + "月" + day + "日";
        } else {
            return year + "年" + month + "月" + day + "日";
        }
    }

    public static String getDynamicTime(long timeMillis) {

        int currentYear = Calendar.getInstance().get(Calendar.YEAR);
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(timeMillis);
        int day = c.get(Calendar.DAY_OF_MONTH);
        int month = c.get(Calendar.MONTH) + 1;
        int year = c.get(Calendar.YEAR);
        String hour = c.get(Calendar.HOUR_OF_DAY) + "";
        String min = c.get(Calendar.MINUTE) + "";
        if (min.length() == 1) {
            min = "0" + min;
        }
        if (hour.length() == 1) {
            hour = "0" + hour;
        }

        if (currentYear == year) {
            return month + "-" + day + " " + hour + ":" + min;
        } else {
            return year + "-" + month + "-" + day + " " + hour + ":" + min;
        }
    }

    public static String getMessageTime(long timeMillis) {

        int currentYear = Calendar.getInstance().get(Calendar.YEAR);
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(timeMillis);
        int day = c.get(Calendar.DAY_OF_MONTH);
        int month = c.get(Calendar.MONTH) + 1;
        int year = c.get(Calendar.YEAR);
        String hour = c.get(Calendar.HOUR_OF_DAY) + "";
        String min = c.get(Calendar.MINUTE) + "";
        String hourTemp = hour;
        if (hourTemp.length() == 1) {
            hourTemp = "0";
        } else {
            hourTemp = "";
        }
        if (min.length() == 1) {
            min = "0" + min;
        }
        if (currentYear == year) {
            return month + "月" + day + "日 " + hourTemp + hour + ":" + min;
        } else {
            return year + "年" + month + "月" + day + "日 " + hourTemp + hour + ":" + min;
        }

    }

    public static String getChatTime(long time, boolean isDetail) {
        // 定义最终返回的结果字符串。
        String interval = "";
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(time);

        int hour = c.get(Calendar.HOUR_OF_DAY);
        int day = c.get(Calendar.DAY_OF_MONTH);
        int month = c.get(Calendar.MONTH);
        int year = c.get(Calendar.YEAR);
        String min = c.get(Calendar.MINUTE) + "";
        String temp = c.get(Calendar.HOUR_OF_DAY) + "";
        if (temp.length() == 1) {
            temp = "0";
        } else {
            temp = "";
        }
        if (min.length() == 1) {
            min = "0" + min;
        }
        String AP = "";

        if (hour < 6) {
            AP = "凌晨 ";
        } else if (hour < 12) {
            AP = "上午 ";
        } else if (hour < 18) {
            AP = "下午 ";
        } else {
            AP = "晚上 ";
        }

        if (Calendar.getInstance().get(Calendar.YEAR) == year) {// 同年
            if (Calendar.getInstance().get(Calendar.MONTH) == month) {// 同月
                if (Calendar.getInstance().get(Calendar.DAY_OF_MONTH) == day) { //同日
                    interval = AP + temp + hour + ":" + min;
                } else if (Calendar.getInstance().get(Calendar.DAY_OF_MONTH) == day + 1) {
                    interval = "昨天 " + temp + hour + ":" + min;
                } else {
                    if (isDetail) {
                        interval = (month + 1) + "月" + day + "日 " + temp + hour + ":" + min;
                    } else {
                        interval = (month + 1) + "月" + day + "日";
                    }
                }
            } else {
                if (isDetail) {
                    interval = (month + 1) + "月" + day + "日 " + temp + hour + ":" + min;
                } else {
                    interval = (month + 1) + "月" + day + "日";
                }
            }
        } else {
            interval = year + "年" + (month + 1) + "月" + day + "日";
        }

        return interval;
    }

    public static int getExpired(String expired){
        // 设置天数
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        long time1 = cal.getTimeInMillis();
        // 转格式
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT);
        expired = formatTime(stringToData(expired,FORMAT));
        Date date;
        try {
            date = sdf.parse(expired);
            cal.setTime(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        int expiredInt = Integer.parseInt(String.valueOf(between_days));
        return expiredInt;
    }

    *//**
     * 将时间字符串截取到年月日
     *//*
    public static String getTime(String dateString) {
        String time = "";
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = df.parse(dateString);
            SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd");
            time = dfs.format(date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return time;
    }

    *//**
     * 将时间字符串截取到年月日时分
     *//*
    public static String getTimeOne(String dateString) {
        String time = "";
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = df.parse(dateString);
            SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            time = dfs.format(date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return time;
    }*/
}
